﻿using EShop.Net.IServices.Advert;
using EShop.Net.Loging;
using EShop.Net.Model;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.FileProviders;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace EShop.Net.API.Controllers.User
{
    /// <summary>
    /// 广告表控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class AdvertController : ControllerBase
    {
        private readonly IAdvertService iadvertService;
        private readonly IWebHostEnvironment webHostEnvironment;

        public AdvertController(IAdvertService iadvertService, IWebHostEnvironment webHostEnvironment)
        {
            this.iadvertService = iadvertService;
            this.webHostEnvironment = webHostEnvironment;
        }

        /// <summary>
        /// 首页获取广告图
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResponseResultJson> GetAdvert()
        {
            try
            {
                // 项目根路径
                IFileProvider rootPath = this.webHostEnvironment.ContentRootFileProvider;
                // 项目域名
                string host = Request.Host.Value;
                var data = await iadvertService.SelectAdvert(host, rootPath);
                return ResponseResultJson.ResponseResult(200, "Success", data);
            }
            catch (Exception e)
            {
                NLogUtil.WriteAll(NLog.LogLevel.Error, LogType.DataBase, "首页获取广告图", "GetAdvert控制器错误", e);
                return ResponseResultJson.ResponseResult(500, "首页获取广告图：" + e.Message, null);
            }
        }
    }
}
